/* 1057:FILE MAPPING */
/*
It is often helpful for computer users to see a visual representation of the file structure on their computers. The "explorer" in Microsoft Windows is an example of such a system. Before the days of graphical user interfaces, however, such visual representations were not possible. The best that could be done was to show a static "map"of directories and files, using indentation as a guide to directory contents.
This shows that the root directory contains two files and three subdirectories. The first subdirectory contains 3 files, the second is empty and the third contains one file.
输入
Write a program that reads a series of data sets representing a computer file structure. A data set ends with a line containing a single *, and the end of valid data is denoted by a line containing a single #. The data set contains a series of file and directory names. (The root directory is assumed to be the starting point.) The end of a directory is denoted by a ']' Directory names begin with a lower case 'd' and file names begin with a lower case 'f' File names may or may not have an extension (such as fmyfile.dat or fmyfile). File and directory names may not contain spaces.
输出
Note that the contents of any directory should list any subdirectories first, followed by files, if any. All files should be in alphabetical order within each directory. Note that each data set output is marked by the label "DATA SET x:" where x denotes the number of the set, beginning at 1. Note also the blank line between the output data sets. Each level of indentation should show a '|' followed by 5 spaces.
*/

#include <iostream>
#include <string.h>

using namespace std;

typedef struct n{
    char temp[64];
    struct n* next;
} node,*pnode;

void insert (pnode p1,pnode p2)
{
    pnode ptemp1 = p1;
    while (ptemp1->next!=NULL)
    {
        if (strcmp(ptemp1->next->temp,p2->temp)>0)
        {
            p2->next=ptemp1->next;
            ptemp1->next=p2;
            return;
        }
        ptemp1=ptemp1->next;
    }
    p2->next=ptemp1->next;
    ptemp1->next=p2;
    return;
}

int calc (unsigned char depth, unsigned char nset)
{
    pnode p=NULL,p1=new node;
    p1->next=NULL;
    while (1)
    {
        p = new node;
        cin>>p->temp;
        if (p->temp[0]=='#')
        {
            return 0;
        }
        if (depth == 1 && p1->next == NULL)
        {
            cout<<"DATA SET "<<(unsigned int)nset<<':'<<endl;
            cout<<"ROOT"<<endl;
        }
        if (p->temp[0]=='*' || p->temp[0]==']')
        {
            while (p1->next != NULL)
            {
                for (int i = 0;i<depth-1;i++)
                {
                    cout<<"|     ";
                }
                cout<<p1->next->temp<<endl;
                p1 = p1->next;
            }
            if (depth == 1)
            {
                cout<<endl;
            }
            break;
        }
        else
        {
            if (p->temp[0]=='f')
            {
                insert(p1,p);
            }
            else if (p->temp[0]=='d')
            {
                for (int i = 0;i<depth;i++)
                {
                    cout<<"|     ";
                }
                cout<<p->temp<<endl;
                calc(depth+1,nset);
            }
        }
    }
    return 1;
}

int main ()
{
    int ret= -1;
    unsigned char nset = 1;
    while (ret != 0)
    {
        ret = calc(1, nset);
        nset++;
    }
}

// Accepted
